package com.ricky.game.scene;

import com.ricky.game.GameWorld;
import com.ricky.game.Hero;
import com.ricky.game.MsgComressionHelper;
import com.ricky.game.Utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 
 * 房间控制器
 * @author rik
 */
public class RoomController extends SceneBase {

	/**
	 * id自增
	 */
	public static AtomicInteger IDGenerate=new AtomicInteger();
	
	@Override
	public Boolean prevEnter(Hero hero, int x, int y, StringBuilder sb) {

		return true;
	}

	@Override
	public void joinWaitingRoom(Hero hero, int x, int y, int z) {
		waiting.put(hero.getName(), hero);//放入等待列表
		hero.setWaiting(this);
	}

	@Override
	public void enter(Hero hero) {
		removeFromWaitingRoom(hero);
		this.online.put(hero.getName(), hero);
		hero.setCurrentScene(this);
		if(waiting.size()==0) //所有玩家都加载完成
		{
//			this.isLocked=true;
			Map<String,Object> data1=new HashMap<String,Object>();
			data1.put("cmd", "r.getHero");
			
			List<Object>  heroList=new ArrayList<Object> ();
			Collection<Hero> heros=this.getOnline().values();
			for(Hero h1:heros)
			{
				Object info=MsgComressionHelper.heroDataMsg(h1);
				heroList.add(info);
			}

			data1.put("arg", Utils.changeListToArray(heroList));
			
			this.broadcast(null, data1);
		}
	}

	@Override
	public void removeFromWaitingRoom(Hero hero) {
		waiting.remove(hero.getName());//移除等待列表
		hero.setWaiting(null);
	}

	@Override
	public void remove(Hero hero) {
		removeFromWaitingRoom(hero);
		online.remove(hero.getName());
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("cmd", "r.remove");
		data.put("name",hero.getName());
		this.broadcast(null, data);
	}

	@Override
	public void synchrostep(Hero hero) {
		
	}

	@Override
	public void update() {

	}

	private boolean isLocked=false;

	public boolean isLocked() {
		return isLocked;
	}

	public void setLocked(boolean isLocked) {
		this.isLocked = isLocked;
	}
	
}
